package cn.aitstone.pet.controller;

import cn.aitstone.basic.exception.BusinessException;
import cn.aitstone.basic.utils.AjaxResult;
import cn.aitstone.basic.utils.LoginContext;
import cn.aitstone.basic.utils.PageList;
import cn.aitstone.org.domain.Employee;
import cn.aitstone.org.domain.ShopAuditLog;
import cn.aitstone.org.service.IEmployeeService;
import cn.aitstone.pet.domain.SearchMasterMsg;
import cn.aitstone.pet.dto.HandleMsgDto;
import cn.aitstone.pet.query.SearchMasterMsgQuery;
import cn.aitstone.pet.service.ISearchMasterMsgService;
import cn.aitstone.user.domain.Logininfo;
import cn.aitstone.user.domain.User;
import cn.aitstone.user.mapper.UserMapper;
import cn.aitstone.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IEmployeeService employeeService;



    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }


    /**
    * @Title: publish
    * @Description: 发布宠物接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/19 17:19
    * @Parameters: [searchMasterMsg, req]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest req){

        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(req);
            User user = userService.loadByLogininfoId(loginInfo.getId());
            searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg, user);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
    * @Title: toAudit
    * @Description: 查询待审核消息
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/20 12:41
    * @Parameters: [searchMasterMsgQuery]
    * @Return cn.aitstone.basic.utils.PageList<cn.aitstone.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery) {
        // 1.设置寻主消息状态为待审核
        searchMasterMsgQuery.setState(0);
        // 2.查询待审核的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }

    /**
    * @Title: tohandle
    * @Description: 查询待处理消息
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/20 12:47  
    * @Parameters: [searchMasterMsgQuery, request]
    * @Return cn.aitstone.basic.utils.PageList<cn.aitstone.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> tohandle(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        // 1.设置寻主消息状态为待审核，设置店铺Id
        searchMasterMsgQuery.setState(1);
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        Employee employee = employeeService.loadByLogininfoId(loginInfo.getId());
        searchMasterMsgQuery.setShopId(employee.getShopId());
        // 2.查询待审核的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }


    /**
    * @Title: pool
    * @Description: 查询寻主池消息
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/20 12:48
    * @Parameters: [searchMasterMsgQuery]
    * @Return cn.aitstone.basic.utils.PageList<cn.aitstone.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery) {
        // 1.设置寻主消息状态为待审核
        searchMasterMsgQuery.setState(1);
        // 2.查询待审核的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }


    /**
    * @Title: finish
    * @Description: 查询已完成的寻主消息
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/20 12:49  
    * @Parameters: [searchMasterMsgQuery]
    * @Return cn.aitstone.basic.utils.PageList<cn.aitstone.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery) {
        // 1.设置寻主消息状态为待审核
        searchMasterMsgQuery.setState(2);
        // 2.查询待审核的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }


    /**
    * @Title: userSearchMasterMsg
    * @Description: 前台用户根据状态查询自己发布的寻主消息
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/20 12:53  
    * @Parameters: [searchMasterMsgQuery, request]
    * @Return cn.aitstone.basic.utils.PageList<cn.aitstone.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> userSearchMasterMsg(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        // 1.设置当前登录用户Id

        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        User user = userService.loadByLogininfoId(loginInfo.getId());
        searchMasterMsgQuery.setUserId(user.getId());
        // 2.查询待审核的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }


    /**
    * @Title: accept
    * @Description: 接单接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/20 16:36
    * @Parameters: [handleMsgDto]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandleMsgDto handleMsgDto){
        try {
            searchMasterMsgService.accept(handleMsgDto);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
    * @Title: reject
    * @Description: 拒单接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/20 21:16
    * @Parameters: [handleMsgDto]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }



}
